package com.syh.jn.scm.infrastructure.repository.impl.upstream.base;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.upstream.entity.base.UpBusinessEntityInvoiceInfo;
import com.syh.jn.scm.domain.upstream.repository.base.UpInvoiceInfoRepository;
import com.syh.jn.scm.infrastructure.repository.model.base.BusinessEntityInvoiceInfoModel;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UpInvoiceInfoRepositoryImpl extends AbstractAggregateRepository<UpBusinessEntityInvoiceInfo, BusinessEntityInvoiceInfoModel, Long> implements UpInvoiceInfoRepository {

  @Override
  public Long add(UpBusinessEntityInvoiceInfo upBusinessEntityInvoiceInfo) {
    BusinessEntityInvoiceInfoModel businessEntityAddressModel = new BusinessEntityInvoiceInfoModel();
    ExtBeanUtils.copyProperties(upBusinessEntityInvoiceInfo, businessEntityAddressModel);
    businessEntityAddressModel.setTerminalType("UPSTREAM");
    return (Long) this.hibernateTemplate.save(businessEntityAddressModel);
  }

  @Override
  public int getCount(Wrapper wrapper) {
    wrapper.append(Restrictions.eq("terminalType", "UPSTREAM"));
    return super.getCount(wrapper);
  }

  /**
   * 内部方法，所有列表查询都调用本方法 改方法是Hibernate实现 注意增删改 业务必须调用该接口数据 不能用view后缀的数据
   *
   * @param wrapper
   * @param startRow
   * @param count
   * @return
   */
  @Override
  protected List<UpBusinessEntityInvoiceInfo> superFindListPage(Wrapper wrapper, Integer startRow, Integer count) {
    wrapper.append(Restrictions.eq("terminalType", "UPSTREAM"));
    return super.superFindListPage(wrapper, startRow, count);
  }

  /**
   * 内部方法，所有Sql执行的列表查询都调用本方法 改方法是 Mybatis 实现 注意增删改 业务不能调用该接口数据
   *
   * @param wrapper
   * @param startRow
   * @param count
   * @return
   */
  @Override
  protected <T> List<T> superFindListPageView(Wrapper wrapper, Integer startRow, Integer count) {
    wrapper.append(Restrictions.eq("terminalType", "UPSTREAM"));
    return super.superFindListPageView(wrapper, startRow, count);
  }

  @Override
  public int getCountView(Wrapper wrapper) {
    wrapper.append(Restrictions.eq("terminalType", "UPSTREAM"));
    return super.getCountView(wrapper);
  }
}
